home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / MUL.C < prev    next >
C/C++ Source or Header  |  1992-02-03  |  7KB  |  251 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/mul.c,v 9.32 1992/02/03 23:33:05 jinx Exp $
  4.  
  5. Copyright (c) 1987-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* This file contains the fixnum multiplication procedure.  Returns
  36.    SHARP_F if the result does not fit in a fixnum.  Note: The portable
  37.    version has only been tried on machines with long = 32 bits.  This
  38.    file is included in the appropriate os file. */
  39.  
  40. extern SCHEME_OBJECT
  41.   EXFUN (Mul, (SCHEME_OBJECT, SCHEME_OBJECT));
  42.  
  43. #if (TYPE_CODE_LENGTH == 8)
  44.  
  45. #if defined(vax) && defined(_BSD)
  46.  
  47. #define MUL_HANDLED
  48.  
  49. /* Note that "register" is used here (not "fast") since the
  50.    assembly code requires knowledge of the location of
  51.    the variables and they therefore must be in registers.
  52.    This is a kludge.  It depends on what register variables
  53.    get assigned to what registers.  It should be entirely
  54.    coded in assembly language.  -- JINX
  55.  
  56.    With gcc, we do have a half-way decent interface to assembly
  57.    code, so the register-assignment dependency is removed.  -- KR
  58. */
  59.  
  60. SCHEME_OBJECT
  61. DEFUN (Mul, (Arg1, Arg2),
  62.        SCHEME_OBJECT Arg1
  63.        AND SCHEME_OBJECT Arg2)
  64. {
  65.   register long A = (FIXNUM_TO_LONG (Arg1));
  66.   register long B = (FIXNUM_TO_LONG (Arg2));
  67. #if __GNUC__
  68. #if FALSE
  69.   /* GCC isn't yet efficient enough with `long long' -- KR.  */
  70.   {
  71.     register long long X;
  72.     asm ("emul %1,%2,$0,%0" : "=g" (X) : "g" (A), "g" (B));
  73.     return
  74.       ((((X & (-1 << 23)) == 0) ||
  75.     ((X & (-1 << 23)) == (-1 << 23)))
  76.        ? (LONG_TO_FIXNUM ((long) X))
  77.        : SHARP_F);
  78.   }
  79. #else
  80.   /* non-long-long version: */
  81.   {
  82.     register struct
  83.       {
  84.     long low;
  85.     long high;
  86.       } X;
  87.     asm ("emul %1,%2,$0,%0" : "=g" (X) : "g" (A), "g" (B));
  88.     B = (X . low);
  89.     A = (X . high);
  90.   }
  91. #endif
  92. #else /* not __GNUC__ */
  93.   asm("    emul r11,r10,$0,r10");  /* A is in 11, B in 10 */
  94. #endif
  95.   /* A should have high order result, B low order */
  96.   return
  97.     ((((A == 0)  && (B & (-1 << 23)) == 0) ||
  98.       ((A == -1) && (B & (-1 << 23)) == (-1 << 23)))
  99.      ? (LONG_TO_FIXNUM (B))
  100.      : SHARP_F);
  101. }
  102.  
  103. #endif /* vax and _BSD */
  104.  
  105. /* 68k family code.  Uses hp9000s300 conventions for the new compiler. */
  106.  
  107. #if defined(hp9000s300) && !defined(old_cc) && !defined(__GNUC__)
  108. #define MUL_HANDLED
  109.  
  110. /* The following constants are hard coded in the assembly language
  111.  * code below.  The code assumes that d0 and d1 are scratch registers
  112.  * for the compiler.
  113.  */
  114.  
  115. #if (SHARP_F != 0) || (TC_FIXNUM != 0x1A)
  116. #include "Error: types changed.  Change assembly language appropriately"
  117. #endif
  118.  
  119. #ifndef MC68010 /* MC68020, MC68030, or MC68040 */
  120.  
  121. static long Fixnum_Range[2] = {SMALLEST_FIXNUM , BIGGEST_FIXNUM};
  122.  
  123.     asm("    text");
  124.     asm("    global _Mul");
  125.     asm("_Mul:");
  126.     asm("    bfexts    4(%sp){&8:&24},%d0");
  127.     asm("    bfexts    8(%sp){&8:&24},%d1");
  128.     asm("    muls.l    %d1,%d0");
  129.     asm("    bvs.b    result_is_nil");
  130.     asm("    cmp2.l    %d0,_Fixnum_Range");
  131.     asm("    bcs.b    result_is_nil");
  132.     asm("    moveq    &0x1A,%d1");
  133.     asm("    bfins    %d1,%d0{&0:&8}");
  134.     asm("    rts");
  135.     asm("result_is_nil:");
  136.     asm("    clr.l    %d0");
  137.     asm("    rts");
  138.     asm("    data");
  139.  
  140. #else    /* MC68010 */
  141.  
  142.     /* 20(sp) = arg0; 24(sp) = arg1 because of movem */
  143.  
  144.     asm("    text");
  145.     asm("    global _Mul");
  146.     asm("_Mul:");
  147.     asm("    movem.l    %d2-%d5,-(%sp)");
  148.     asm("    clr.b    %d5");
  149.     asm("    tst.b    21(%sp)");
  150.     asm("    slt    20(%sp)");
  151.     asm("    bge.b    coerce_1");
  152.     asm("    moveq    &1,%d5");
  153.     asm("    neg.l    20(%sp)");
  154.  
  155.     asm("coerce_1:");
  156.     asm("    tst.b    25(%sp)");
  157.     asm("    slt    24(%sp)");
  158.     asm("    bge.b    after_coerce");
  159.     asm("    eori.b    &1,%d5");
  160.     asm("    neg.l    24(%sp)");
  161.     asm("after_coerce:");
  162.     asm("    move.l    20(%sp),%d0");
  163.     asm("    move.l    24(%sp),%d1");
  164.     asm("    move.w    %d0,%d2");
  165.     asm("    mulu    %d1,%d2");
  166.     asm("    move.w    %d1,%d4");
  167.     asm("    swap    %d1");
  168.     asm("    move.w    %d1,%d3");
  169.     asm("    mulu    %d0,%d3");
  170.     asm("    swap    %d0");
  171.     asm("    mulu    %d0,%d4");
  172.     asm("    add.l    %d4,%d3");
  173.     asm("    bcs.b    result_is_nil");
  174.     asm("    mulu    %d0,%d1");
  175.     asm("    bne.b    result_is_nil");
  176.     asm("    swap    %d2");
  177.     asm("    add.w    %d3,%d2");
  178.     asm("    bcs.b    result_is_nil");
  179.     asm("    swap    %d3");
  180.     asm("    tst.w    %d3");
  181.     asm("    bne.b    result_is_nil");
  182.     asm("    cmpi.w    %d2,&0x7F");
  183.     asm("    bgt.b    result_is_nil");
  184.     asm("    swap    %d2");
  185.     asm("    tst.b    %d5");
  186.     asm("    beq.b    sign_is_right");
  187.     asm("    neg.l    %d2");
  188.     asm("sign_is_right:");
  189.     asm("    move.l    %d2,-(%sp)");
  190.     asm("    move.b    &0x1A,(%sp)");
  191.     asm("    move.l    (%sp)+,%d0");
  192.     asm("    movem.l    (%sp)+,%d2-%d5");
  193.     asm("    rts");
  194.     asm("result_is_nil:");
  195.     asm("    clr.l    %d0");
  196.     asm("    movem.l    (%sp)+,%d2-%d5");
  197.     asm("    rts");
  198.     asm("    data");
  199.  
  200. #endif    /* MC68010 */
  201. #endif  /* hp9000s300 */
  202.  
  203. #endif /* (TYPE_CODE_LENGTH == 8) */
  204.  
  205. #ifndef MUL_HANDLED
  206.  
  207. #define HALF_WORD_SIZE    ((sizeof(long)*CHAR_BIT)/2)
  208. #define HALF_WORD_MASK    (1<<HALF_WORD_SIZE)-1
  209. #define MAX_MIDDLE    (1<<((DATUM_LENGTH-1)-HALF_WORD_SIZE))
  210. #define MAX_FIXNUM    (1<<DATUM_LENGTH)
  211. #define    ABS(x)        (((x) < 0) ? -(x) : (x))
  212.  
  213. SCHEME_OBJECT
  214. DEFUN (Mul, (Arg1, Arg2),
  215.        SCHEME_OBJECT Arg1
  216.        AND SCHEME_OBJECT Arg2)
  217. {
  218.   long A, B, C;
  219.   fast unsigned long Hi_A, Hi_B, Lo_A, Lo_B, Lo_C, Middle_C;
  220.   Boolean Sign;
  221.  
  222.   A = (FIXNUM_TO_LONG (Arg1));
  223.   B = (FIXNUM_TO_LONG (Arg2));
  224.   Sign = ((A < 0) == (B < 0));
  225.   A = ABS(A);
  226.   B = ABS(B);
  227.   Hi_A = ((A >> HALF_WORD_SIZE) & HALF_WORD_MASK);
  228.   Hi_B = ((B >> HALF_WORD_SIZE) & HALF_WORD_MASK);
  229.   if ((Hi_A > 0) && (Hi_B > 0))
  230.     return (SHARP_F);
  231.   Lo_A = (A & HALF_WORD_MASK);
  232.   Lo_B = (B & HALF_WORD_MASK);
  233.   Lo_C = (Lo_A * Lo_B);
  234.   if (Lo_C >= FIXNUM_SIGN_BIT)
  235.     return (SHARP_F);
  236.   Middle_C = (Lo_A * Hi_B) + (Hi_A * Lo_B);
  237.   if (Middle_C >= MAX_MIDDLE)
  238.     return (SHARP_F);
  239.   C = Lo_C + (Middle_C << HALF_WORD_SIZE);
  240.   if (LONG_TO_FIXNUM_P(C))
  241.   {
  242.     if (Sign || (C == 0))
  243.       return (LONG_TO_UNSIGNED_FIXNUM(C));
  244.     else
  245.       return (LONG_TO_UNSIGNED_FIXNUM(MAX_FIXNUM - C));
  246.   }
  247.   return (SHARP_F);
  248. }
  249.  
  250. #endif /* not MUL_HANDLED */
  251.